<script type="text/javascript" src="lang/ch.js"></script>
<script src="static/artDialog/lib/jquery-1.10.2.js" type="text/javascript"></script>
<link href="static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="static/bootstrapQ/qiao.css" rel="stylesheet">
<script src="static/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="static/bootstrapQ/qiao.js" type="text/javascript"></script>
<link href="static/zTree/css/metroStyle/standardStyle.css" rel="stylesheet" id="zTreeCss">
<style type="text/css">
  	/* 调整zTree每行的高度 */
	.ztree li a {
          /*height: 25px !important;*/
          width: 90% !important;
      }
	      
      .ztree li .caret {
          margin-top: 8px !important;
      }
</style>
<!-- artDialog -->
<script src="static/artDialog/dist/dialog.js"></script>
<script src="static/artDialog/dist/dialog-plus.js"></script>
<link rel="stylesheet" href="./css/artDialog.css">
<script src="./js/artDialog.js"></script>
<script type="text/javascript" src="static/zTree/js/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="js/DocSys.js"></script>

<!-- contextMenu -->
<link rel="stylesheet" type="text/css" href="static/ContextJS/css/demo.css">
<link rel="stylesheet" type="text/css" href="static/ContextJS/css/context.standalone.css">
<link rel="stylesheet" type="text/css" href="css/zjp-css.css">
<script type="text/javascript" src="static/ContextJS/js/context.js"></script>

<div>
	<ul id="zipPlayer" class="ztree" style="margin-bottom: 120px;"></ul>
</div>

<script type="text/javascript">
	function GetRequest() {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = {};
		if (url.indexOf("?") !== -1) {
			var str = url.substr(1);
			var strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	}

	var params = GetRequest();
	var docid = params['docid'];
	//获取artDialog父窗口传递过来的参数
	var artDialog2 = window.top.artDialogList['ArtDialog' + docid];
	if (artDialog2 == null) {
		artDialog2 = window.parent.artDialogList['ArtDialog' + docid];
	}
	// 获取对话框传递过来的数据
	var docInfo = artDialog2.config.data;
	console.log("docInfo:",docInfo);
	var gShareId = docInfo.shareId;
	var curRightClickedTreeNode = null;
	
	//用于解决artDialog再次调用artDialog的数据穿透问题
	var gDialogData = {};
	
	contextMenuInit();
	
	$(document).ready(function()
	{
		getAndShowZipInitMenu(docInfo);
	});
		
	function getAndShowZipInitMenu(docInfo) 
	{
		console.log("getAndShowZipInitMenu docId:" + docInfo.docId + " parentPath:" + docInfo.path + " docName:" + docInfo.name);
			
		$.ajax({
		    url : "/DocSystem/Doc/getZipInitMenu.do",
		    type : "post",
		    dataType : "json",
		    data : {
		        reposId : docInfo.vid,  
		    	docId:  docInfo.docId,
		    	docPath: docInfo.path,
		    	docName: docInfo.name,
		    	shareId: docInfo.shareId,
		    },
		    success : function (ret) {
		        	if( "ok" == ret.status ){
		        		console.log("getMenu() ret",ret);
		                drawMenu(ret.data);
		            }
		            else
		           	{
	                	showErrorMessage("获取压缩文件列表失败:" + ret.msgInfo);
		           	}
		    },
		    error : function () {
		            showErrorMessage("获取压缩文件列表失败:服务器异常");
		    }
		});
	}			
	
	function reDrawMenu(data)
	{
	   drawMenu(data);
	}
	
	//绘制zTree with the data:强制绘制，判断的东西不应该放在这里
	function drawMenu(data) {
		console.log("drawMenu");
		window.menu = data;
	    var menu = data;
	    //遍历jason_arry
      	for(var i=0; i<menu.length; i++)
      	{
           var jsonObj = menu[i];
           jsonObj.id = jsonObj.docId;
           jsonObj.pId = jsonObj.pid != 0? jsonObj.pid : "root",
           jsonObj.isParent = jsonObj.type == 1? false: true;
           jsonObj.Level = jsonObj.level;
           jsonObj.isZip = 1;
           jsonObj.rootPath = docInfo.path;
           jsonObj.rootName = docInfo.name;
           //设置用户自定义文件图标（必须在standardStyle中有定义）
           if(jsonObj.type == 1)
           {
        	   var iconType = getDiyFileIconType(jsonObj.name);
        	   if(iconType && iconType != "")
        	   {
        		   jsonObj.iconSkin = iconType;
        	   }
           }
       }
	   zTreeInit(menu);
	}
	
	function zTreeInit(data) {
	    console.log("zTreeInit");
	    var async = {  
    		enable : true,//设置 zTree 是否开启异步加载模式  
            url : "/DocSystem/Doc/getZipSubDocList.do",
            type : "post",
    		autoParam : ["id","Level","path","name"],//异步加载时需要自动提交父节点属性的参数  
    		otherParam:{
    			"reposId": docInfo.vid,
		    	"docPath": docInfo.path, //zipFile的路径
		    	"docName": docInfo.name, //zipFile的名字
                "shareId": docInfo.shareId,
    		},
    		dataFilter: asyncDataFilter,
	    };
    	setting.async = async;
	    var doctree = $.fn.zTree.init($("#zipPlayer"), setting, data);
	}
	
	//zTree's setting
	var setting = {
	   	//可编辑功能设置
		edit: {},
           //zTree数据格式
	   	data: {
	   			//使用简单数据模式
	            simpleData: {
	                enable: true,
	            },
	            //不允许修改leaf node and parent node的isParent属性
	            keep: {
	            	leaf: true,
	            	parent: true,
	            },
	    },
        //设置异步加载的参数
        async : {  
    		enable : true,//设置 zTree 是否开启异步加载模式  
            url : "/DocSystem/Doc/getZipSubDocList.do",
            type : "post",
    		autoParam : ["id","Level","path","name"],//异步加载时需要自动提交父节点属性的参数  
    		otherParam:{
    			"reposId": docInfo.vid,
		    	"docPath": docInfo.path, //zipFile的路径
		    	"docName": docInfo.name, //zipFile的名字
                "shareId": docInfo.shareId,
    		},
    		dataFilter: asyncDataFilter, 
		},
	    //zTree各种操作的回调函数定义
	    callback: {
	            beforeAsync: zTreeBeforeAsync, //异步加载前的回调函数， 可以用来判断是否需要异步加载
                onAsyncSuccess: zTreeOnAsyncSuccess, //异步加载完成后的回调
	            onClick: zTreeOnClick,
	            onDblClick: zTreeOnDbClick,
	            beforeRightClick: zTreeBeforeRightClick,
	            //onRightClick: zTreeOnRightClick, //定义该回调将会屏蔽系统右击事件
	    },
	    view: {
        	//showLine: false,	//不显示文件下面的下划线
        	//addDiyDom: addDiyDom         //设置zTree的自定义图标
			addHoverDom: addHoverDom,
			removeHoverDom: removeHoverDom,
        }
	};
	
	function addHoverDom(treeId, treeNode) {
		var aObj = $("#" + treeNode.tId + "_a");
		if ($("#diyBtn_"+treeNode.id).length>0) return;
		var editStr = "<span class='caret pull-right' id='diyBtn_" + treeNode.id
			+ "' title='"+treeNode.name+"' onfocus='this.blur();' onclick='showContextMenu(this,event);'></span>";
		aObj.append(editStr);
	}
	
	function removeHoverDom(treeId, treeNode) {
		$("#diyBtn_"+treeNode.id).unbind().remove();
	}
	
	
    //为了能够让外部接口能够调用zTree的callback，需要记录当前treeNode等变量
    function zTreeOnClick(event, treeId, treeNode) 
    {
    	console.log("zTreeOnClick treeId:"+ treeId, treeNode);
    	
    	//手机端上点击则打开   	
    }
      
      //Double Click 对于文件应该是编辑，对于目录应该是打开
    function zTreeOnDbClick(event, treeId, treeNode) 
    {
    	console.log("zTreeOnDbClick treeId:"+ treeId, treeNode);
    	gDialogData["ArtDialog" + treeNode.docId] = treeNode;
		
    	openDoc(treeNode, true, "openInArtDialog", docInfo.shareId);
    }
	
      //This function was used to get the rightClick treeNode,it will be used for contextjs
	function zTreeBeforeRightClick(treeId, treeNode) {
		console.log("zTreeBeforeRightClick treeId:"+ treeId, treeNode);
		curRightClickedTreeNode = treeNode;
    	return true;	
	}
	
	function zTreeOnRightClick(event, treeId, treeNode) {
		//alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
		showZipTreeContextMenu(event, 0, 0);
	}
      
    function zTreeBeforeAsync(treeId, treeNode) {  
      	console.log("zTreeBeforeAsync treeId:"+ treeId, treeNode);
    }  
        
    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
      	console.log("zTreeOnAsyncSuccess treeId:" + treeId);
    }
  	function asyncDataFilter(treeId, parentNode, responseData) {
		console.log("asyncDataFilter");
		var docList = responseData.data;
		if(!docList)
		{
			return docList;
		}
		//遍历jason_arry, convert the node type to isParent flag
      	for(var i=0; i<docList.length; i++)
      	{
           var jsonObj = docList[i];
           jsonObj.id = jsonObj.docId;
           jsonObj.pId = jsonObj.pid != 0? jsonObj.pid : "root",
           jsonObj.isParent = jsonObj.type == 1? false: true;
           jsonObj.isZip = 1;
           jsonObj.rootPath = docInfo.path;
           jsonObj.rootName = docInfo.name;
           //设置用户自定义文件图标（必须在standardStyle中有定义）
           if(jsonObj.type == 1)
           {
        	   var iconType = getDiyFileIconType(jsonObj.name);
        	   if(iconType && iconType != "")
        	   {
        		   jsonObj.iconSkin = iconType;
        	   }
           }
       }
       console.log(docList);
	   return docList;
	}
  	
  	function zipOpenDoc()
  	{
  		console.log("zipOpenDoc:", curRightClickedTreeNode);
	  	if(curRightClickedTreeNode && curRightClickedTreeNode != null)
  		{
	    	gDialogData["ArtDialog" + curRightClickedTreeNode.docId] = curRightClickedTreeNode;
	  		openDoc(curRightClickedTreeNode, true, "openInArtDialog", docInfo.shareId);
  		}
  	}
  	
  	function zipDownloadDoc()
  	{
  		console.log("zipDownloadDoc:", curRightClickedTreeNode);
  		getDocFileLink(curRightClickedTreeNode, doDownloadDoc, showErrorMessage, "REST"); //要求获取RESTFUL风格的fileLink
  	}
  	
  	function zipShowDocDetail()
  	{
  		console.log("zipShowDocDetail:", curRightClickedTreeNode);
  		showDocDetail(curRightClickedTreeNode); 		
  	}
	   	
	function doDownloadDoc(fileLink)
	{   	
	 	console.log("doDownloadDoc fileLink: " + fileLink);
	   	window.location.href = fileLink;
	}
	
 	function formatTime(time){
 		var now = new Date(time);
 		var year=now.getFullYear(); 
 		var month=now.getMonth()+1;
 		var date=now.getDate(); 
 		var hh=now.getHours(); 
 		var mm=now.getMinutes(); 
 		var ss=now.getSeconds(); 
 		
 		return year+"-"+month+"-"+date + " " + hh+":"+mm+":"+ss; 
 	}
 	
 	function showContextMenu(obj,event){
 		event.stopPropagation();
 		context.show('#zipPlayer',event, -150);
 	}
 	
    function contextMenuInit()
    {
		//右键菜单实现：contextMenu Start
		context.init({preventDoubleContext: true});
		
		//zTree上的右键菜单
		context.attach('#zipPlayer', [
			{text: '打开', action: function(e){
					e.preventDefault();
					zipOpenDoc();
				}
			},
			{text: '下载', action: function(e){
						e.preventDefault();
						zipDownloadDoc();
					}
			},
			{divider: true},
			{text: '详细信息', action: function(e){
					e.preventDefault();
					zipShowDocDetail();
				}
			},
		]);
		
		context.settings({compress: true});
		
		$(document).on('mouseover', '.me-codesta', function(){
			$('.finale h1:first').css({opacity:0});
			$('.finale h1:last').css({opacity:1});
			});
		
		$(document).on('mouseout', '.me-codesta', function(){
			$('.finale h1:last').css({opacity:0});
			$('.finale h1:first').css({opacity:1});
		});
		//右键菜单实现：contextMenu End
    }
</script>
